package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// 通过 BlogDao 来完成对 blog 表的操作
public class BlogDao {
    //1. 新增操作 (提交博客中用到)
    public void insert(Blog blog){
        //TODO
        Connection connection = null;
        PreparedStatement statement = null;  // 把这两个定义到外面
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构造sql语句
            String sql = "insert into blog values(null,?,?,now(),?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            // 3. 执行sql
            statement.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
    // 2. 查询博客列表 (博客列表页用到)
    //  把数据库里面的所有博客都拿到
    public List<Blog> getBlogs(){
        //TODO
        List<Blog> blogList = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet =null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            resultSet =  statement.executeQuery();
            while (resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                // 此处读到的正文是整个文章的内容,太多了, 博客列表,只是希望显示一小部分(摘要)
                // 此处需要对 content 做一个简单的截断 .
                String content = resultSet.getString("content");
                // 这个截断长度100 是自己随便想出来的, 具体截取多少可以自己灵活调整
                if(content.length() > 100){
                    content = content.substring(0,100) + "...";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                blogList.add(blog);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return blogList;
    }

    // 3.根据博客 id 查询指定的博客
    // 这里面只需要返回一个博客对象就可以了
    public Blog getBlog(int blogId){
        //TODO
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection=DBUtil.getConnection();
            String sql = " select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            resultSet = statement.executeQuery();
            if (resultSet.next()){  // 由于此处 是拿着 blogId 来进行查询, 但是 blogId 作为 主键是唯一的
                // 所以 查询结果 非 0 即 1 , 不需要使用 while 来进行遍历
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                // 这个方法是期望 获取博客详情页的时候调用的 , 所以此处不需要进行截断, 应该展示完整的
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return  blog;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    // 4. 根据 博客id 删除博客
    public void delete(int blogId){
        //TODO
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection=DBUtil.getConnection();
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            statement.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
}
